﻿#include "Impediments.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Config.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
USING_NS_CC_EXT;

void Impediments::initValueForAttribute(CCPoint point, int type)
{
	mTimeBeforeAppear = kImpedimentTimeBeforeAppear;
	mTimeAppear = kImpedimentTimeAppear;
	mType = type;
	mAppear = kImpedimentDisappear;
	mBroken = kImpedimentNotBroken;
	setPosition(point);


	/*CCSize nSize = CCDirector::sharedDirector()->getWinSize();
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
	CCSize nSizeScreen = pEGLView->getFrameSize();*/

	CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(fSelectObjectImage(), fSelectObjectPlist(), fSelectObjectJson());
	armature = CCArmature::create(fSelectObjectName()); 
    armature->setPosition(ccp(0,0));
	//float nScale = fSelectObjectScale();
	//armature->setScaleX(nScale);
	armature->setScaleY(fAutoSizeImpediment());
    this->addChild(armature,500); 
	armature->getAnimation()->play("loading"); 
	// Pre load music
}

// Animation callback
void Impediments::onAnimationEvent(cocos2d::extension::CCArmature *pArmature, cocos2d::extension::MovementEventType eventType, const char *animationID)
{
	std::string id = animationID;
    if (eventType == COMPLETE)
    {
		/*CCLog("Animation complete");
		CCLog("Animation ID = %d",animationID);*/

        if (id.compare("broken") == 0)
        {	
			//CCLog("It's broken !!");
			pArmature->getAnimation()->stop();// Stop animation when loop complate
        }
    }

}

CCArmature * Impediments::getArmature()
{
	return this->armature;
}

Impediments::Impediments(CCPoint point, int type)
{
	
	initValueForAttribute(point , type);

}
Impediments::Impediments(int type)
{

	initValueForAttribute(ccp(0,0), type);

	// Pre load music
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kSoundBroken);
}
Impediments::Impediments()
{
	initValueForAttribute(ccp(0,0),0);
}

char* Impediments::fSelectObjectName()
{
	switch(mType)
	{
		case kBowl: return kArmatureBowlName; // Cai bat
		case kCupOfCaffe: return kArmatureCupOfCaffeName;
		case kBowlOfFruit: return kArmatureBowlOfFrutName;
		case kBowlOfBread: return kArmatureBowlOfBreadName;
		default: return kArmatureBowlName;
	}
}
char* Impediments::fSelectObjectImage()
{
	switch(mType)
	{
		case kBowl: return kArmatureBowl; // Cai bat
		case kCupOfCaffe: return kArmatureCupOfCaffe;
		case kBowlOfFruit: return kArmatureBowlOfFrut;
		case kBowlOfBread: return kArmatureBowlOfBread;
		default: return kArmatureBowl;
	}
}
char* Impediments::fSelectObjectJson()
{
	switch(mType)
	{
		case kBowl: return kArmatureBowlJson; // Cai bat
		case kCupOfCaffe: return kArmatureCupOfCaffeJson;
		case kBowlOfFruit: return kArmatureBowlOfFrutJson;
		case kBowlOfBread: return kArmatureBowlOfBreadJson;
		default: return kArmatureBowlJson;
	}
}
char* Impediments::fSelectObjectPlist()
{
	switch(mType)
	{
		case kBowl: return kArmatureBowlPList; // Cai bat
		case kCupOfCaffe: return kArmatureCupOfCaffePlist;
		case kBowlOfFruit: return kArmatureBowlOfFrutPlist;
		case kBowlOfBread: return kArmatureBowlOfBreadPlist;
		default: return kArmatureBowlPList;
	}
}
float Impediments::fSelectObjectScale()
{
	/*CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
	CCSize nSizeScreen = pEGLView->getFrameSize();
	CCSize nSize = pEGLView->getDesignResolutionSize();
	float nNormal = nSize.width/nSizeScreen.width;*/

	switch(mType)
	{
		case kBowl: return 0.4; // Cai bat
		case kCupOfCaffe: return 0.2;
		case kBowlOfFruit: return 0.2;
		case kBowlOfBread: return 0.2;
		default: return 0.5;
	}
}
int Impediments::fSelectObjectNumberFrame()
{
	switch(mType)
	{
		case kBowl: return kNumberFrameBowl;
		case kCupOfCaffe: return kNumberFrameCupOfCaffe;
		case kBowlOfFruit: return kNumberFrameBowlOfFrut;
		case kBowlOfBread: return kNumberFrameBowlOfBread;
		default: return kNumberFrameBowl;
	}
}

void Impediments::fSetPosition(CCPoint point)
{
	CCNode::setPosition(point);
}
void Impediments::fSetPosition(float x, float y)
{
	CCNode::setPosition(x,y);
}
void Impediments::fSetAppear(int appear)
{
	this->mAppear = appear;
}
void Impediments::fSetBroken(int broken)
{
	this->mBroken = broken;


}
void Impediments::fSetTimeDisAppearBeforeAppear(int timeDisAppear)
{
	mTimeDisAppearBeforeAppear = timeDisAppear;
}

CCPoint Impediments::fGetPosition()
{
	return CCNode::getPosition();
}
int Impediments::fGetAppear()
{
	return mAppear;
}
int Impediments::fGetBroken()
{
	return mBroken;
}
int Impediments::fGetType()
{
	return mType;
}
int Impediments::fGetTimeDisAppearBeforeAppear()
{
	return mTimeDisAppearBeforeAppear;
}

void Impediments::fRunEffectsBroken()
{
	///Chay hieu ung vo nat
	if(mBroken == kImpedimentBroken)
	{
		//this->runAction(fEffectsBroken());
		fEffectsBroken();
	}
	mTimeAppear = kImpedimentTimeAppear;
	schedule(schedule_selector(Impediments::fRunUpdate), 1.0f);
}
void Impediments::fEffectsBroken()
{

	switch(mType)
	{
		case kBowl: 
			//CCLog("Bat vo");
			armature->getAnimation()->play("broken"); 
			// play sound effect here
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;
		case kCupOfCaffe:
			armature->getAnimation()->play("broken"); 
			// play sound effect here
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			//CCLog("Ly cafe vo");
			break;
		case kBowlOfFruit:
			armature->getAnimation()->play("broken"); 
			// play sound effect here
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			//CCLog("Nuoc trai cay vo");
			break;
		case kBowlOfBread: 
			armature->getAnimation()->play("broken"); 
			// play sound effect here
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			//CCLog("Banh my vo");

			break;
		default: 
			//CCLog("Default effect broken !!");
			break;
	}
}

void Impediments::fRunEffectsAppear(int time, int number)
{
	this->setVisible(true);
	if(mAppear == kImpedimentDisappear)
	{
		this->runAction(fEffectsAppear(time, number));
	}
	/*mTimeBeforeAppear = kImpedimentTimeBeforeAppear;
	schedule(schedule_selector(Impediments::fRunUpdateAppear), 1.0f);*/
}
CCActionInterval* Impediments::fEffectsAppear(int time, int number)
{
	CCActionInterval *nAction = CCBlink::create(time, number);
	return CCSequence::createWithTwoActions(nAction, CCCallFuncO::create(nAction, callfuncO_selector(Impediments::fRunEffectsDisappear), this));
}

void Impediments::removeSelf(CCObject* sender){
	if (sender){
		Impediments* tmp = (Impediments*)sender;
		tmp->pauseSchedulerAndActions();
		if (tmp->getParent()){
			tmp->removeFromParent();
		}
		tmp = NULL;
	}
}

void Impediments::fRunEffectsDisappear(CCObject* sender){
	
	if (sender){
		((Impediments*)sender)->fSetAppear(kImpedimentAppear);
	}
}

void Impediments::fRunEffectsDisappear(int time)
{
	if(mAppear == kImpedimentAppear)
	{
		mAppear = kImpedimentDisappear;
		CCFadeOut* out = fEffectsDisappear(time);
		this->runAction(CCSequence::createWithTwoActions(out, CCCallFuncO::create(out, callfuncO_selector(Impediments::removeSelf), (CCObject*)this)));
	}
}
CCFadeOut* Impediments::fEffectsDisappear(int time)
{

	CCFadeOut *nFadeOut = CCFadeOut::create(time);
	return nFadeOut;
}

CCActionInterval* Impediments::fEffectsVibrate(float time)
{

	CCActionInterval* pEffect = CCShaky3D::create(time, CCSizeMake(50, 40), 5, false);
	return pEffect;
}

void Impediments::fRunUpdate(float delta)
{
	mTimeAppear--;
	if(fIsBrokenAndDisAppear() == kImpedimentBrokenAndAppear && mTimeAppear == kImpedimetZero)
	{
		fRunEffectsDisappear(kImpedimentTimeDisappear);	
	}
	if(mTimeAppear == - kImpedimentTimeDisappear)
	{
		fSetAppear(kImpedimentDisappear);
	}
}

void Impediments::fRunUpdateAppear(float delta)
{
	mTimeBeforeAppear--;
	if(mTimeBeforeAppear == kImpedimetZero)
	{
		fSetAppear(kImpedimentAppear);
		unschedule(schedule_selector(Impediments::fRunUpdateAppear));
	}
}

int Impediments::fIsBrokenAndDisAppear()
{
	if(mBroken == kImpedimentBroken && mAppear == kImpedimentAppear)
	{
		return kImpedimentBrokenAndAppear;
	}
	else if(mBroken == kImpedimentNotBroken && mAppear == kImpedimentAppear)
	{
		return kImpedimentNotBrokenAndAppear;
	}
	else if(mBroken == kImpedimentNotBroken && mAppear == kImpedimentDisappear)
	{
		return kImpedimentBrokenAndDisAppear;
	}
	return kImpedimentNotBrokenAndDisAppear;
}

float Impediments::fAutoSizeImpediment()
{
	CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
	CCSize nSizeScreen = pEGLView->getFrameSize();
	CCSize nSize = pEGLView->getDesignResolutionSize();
	return ((float)nSize.height/nSizeScreen.height * nSizeScreen.width/nSize.width);
}